﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL = KC.Business.BlogSlinger;
using DAL = KC.DataAccess.BlogSlinger;

public partial class Blog : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (Request.QueryString["ID"] == null)
			Response.Redirect("Browse.aspx");

		int BlogID;
		if (!int.TryParse(Request.QueryString["ID"], out BlogID))
			Response.Redirect(".");

		DAL.Blog blog = BLL.GetBlog(ref Master.dc, BlogID);
		if (blog == null)
			Response.Redirect(".");
		lblBlogTitle.Text = blog.Name;
		Title = "Blog Network > " + blog.Name;

		hlRSS.NavigateUrl = "rss.aspx?ID=" + BlogID.ToString();

		// TODO: Blog.aspx Pagination
		rPosts.DataSource = BLL.GetPostsByBlog(ref Master.dc, BlogID, (int)(Int16.MaxValue)).Where(p => p.Status == "Active");
		rPosts.DataBind();

		BLL.LogActivity(ref Master.dc, Master.user, Request.UserHostAddress, "Browsed Blog", "Blog ID " + BlogID.ToString(), Request.Url.AbsoluteUri, (Request.UrlReferrer != null ? Request.UrlReferrer.AbsoluteUri : ""));
	}
	protected void rPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
			return;
		DAL.Post post = (DAL.Post)e.Item.DataItem;
		
		HyperLink hlTitle = (HyperLink)e.Item.FindControl("hlTitle");
		hlTitle.NavigateUrl = "Post.aspx?Title=" + post.FriendlyName;
		hlTitle.Text = post.Title;

		((Label)e.Item.FindControl("lblPosted")).Text = post.PublishDate.ToString("MM/dd/yyyy HH:mm:ss");
		((Label)e.Item.FindControl("lblBody")).Text = post.Body;

		if (post.isAllowComments) {
			HyperLink hlComments = (HyperLink)e.Item.FindControl("hlComments");
			hlComments.NavigateUrl = "Post.aspx?Title=" + post.FriendlyName;
			hlComments.Text = "Read/Post Comments >";
		}

	}
}